package com.copro.chemdb.stat.rule.export;

import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.ihelpuoo.util.Properties;

import com.copro.chemdb.stat.ExportRule;
import com.copro.chemdb.stat.StatResult;
import com.copro.chemdb.stat.rule.StatOption;
import com.copro.chemdb.subject.Subject;
import com.copro.simple.Application;
import com.copro.simple.SimpleException;

public class Rule1 implements ExportRule {

	@SuppressWarnings("unchecked")
	@Override
	public void doExport(HSSFWorkbook wb, Subject subject, StatResult re,
			short customStyle, Application app) throws SimpleException {

		Map<String, Object> result = (Map<String, Object>) re.getResult();
		String text = (String) result.get("text");
		if (text.length() > 30) {
			text = text.substring(0, 30);
		}
		// create new sheet
		HSSFSheet sheet = wb.createSheet(text);
		HSSFRow row = sheet.createRow(0);
		// set the total number
		HSSFRichTextString richString = new HSSFRichTextString("有效记录数:"
				+ result.get("count"));
		row.createCell((short) 0).setCellValue(richString);
		// set the table header
		row = sheet.createRow(1);
		CellUtil.createCell(row, 0, "", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 1, "数量", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 2, "所占比例%", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		// set the table body
		List<StatOption> options = (List<StatOption>) result
				.get("statOptionSet");
		StatOption option = null;
		int resultCount = ((Integer) result.get("count")).intValue();
		int count = 0;
		for (int i = 0; i < options.size(); i++) {
			option = options.get(i);
			row = sheet.createRow(i + 2);
			count += option.getCount();
			CellUtil.createCell(row, 0, option.getOption().getText());
			CellUtil.createCell(row, 1, option.getCount(), wb
					.getCellStyleAt((short) (customStyle + StyleInt)));
			double percent = resultCount == 0 ? 0 : (double) option.getCount()
					/ resultCount;
			CellUtil.createCell(row, 2, percent, wb
					.getCellStyleAt((short) (customStyle + StylePercent)));
		}
		// set the table footer
		row = sheet.createRow(options.size() + 2);
		CellUtil.createCell(row, 0, "合计", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 1, count, wb
				.getCellStyleAt((short) (customStyle + StyleHeaderInt)));
		CellUtil.createCell(row, 0, "", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));

		// insert the stat chart
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		HSSFClientAnchor anchor = new HSSFClientAnchor();
		anchor.setRow1(options.size() + 4);
		HSSFPicture picture = patriarch.createPicture(anchor,
				wb.addPicture(app.getStatChart(subject.getId()),
						HSSFWorkbook.PICTURE_TYPE_PNG));
		picture.resize();

	}

	@Override
	public void setup(Properties conf) {
	}

}
